//
//  JYPopContainerView.m
//  s.
//
//  Created by fanshijian on 2022/8/31.
//  Copyright © 2022 J. All rights reserved.
//
#define KLWKSelf __weak typeof(self) wkSelf = (self);
#define KLSTSelf __strong typeof(wkSelf)self = (wkSelf);
#import "JYPopContainerView.h"

@interface JYPopContainerView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, weak) UIView *super_view;

@property (nonatomic, copy) JYPopVoidBlock closeBlock;

@end

@implementation JYPopContainerView

- (instancetype)initWithSuperView:(UIView *)superView withView:(UIView *)subView {
    _super_view = superView;
    _subView = subView;
    CGRect rect = superView.bounds;
    if (self = [super initWithFrame:rect]) {
        [self __setup];
    }
    return self;
}

- (void)__setup {
    _isTapBack = YES;
    self.centerOffsetSize = CGSizeZero;
    
    [self addSubview:self.bgView];
    CGRect subRect = self.subView.frame;
    CGSize subSize = subRect.size;
    subRect.origin.x = (self.frame.size.width - subSize.width)/2.0f;
    subRect.origin.y = self.frame.size.height;
    self.subView.frame = subRect;
    
    [self addSubview:self.subView];
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        [_bgView setBackgroundColor:[UIColor clearColor]];
        _bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (void)setBackViewColor:(UIColor *)backViewColor {
    [self.bgView setBackgroundColor:backViewColor];
}

- (void)setBackViewAlpha:(CGFloat)backViewAlpha {
    [self.bgView setAlpha:backViewAlpha];
}

#pragma mark - btn action
- (void)tapAction:(id)sender {
    if (self.isTapBack) {
        [self dissmiss];
    }
}


- (void)showBlock:(JYPopVoidBlock)block {
    _closeBlock = block;
    [self.super_view addSubview:self];
}

- (void)dissmiss {
    KLWKSelf
    if (self.dismissAnimationBlock) {
        self.dismissAnimationBlock(^{
            KLSTSelf
            [self removeFromSuperview];
            self.super_view = nil;
            if (self.closeBlock) {
                self.closeBlock();
            }
        });
    }else {
        [self removeFromSuperview];
        self.super_view = nil;
        if (self.closeBlock) {
            self.closeBlock();
        }
    }
}

- (void)dissmissBlock:(JYPopVoidBlock)block {
    KLWKSelf
    if (self.dismissAnimationBlock) {
        self.dismissAnimationBlock(^{
            KLSTSelf
            [self removeFromSuperview];
            self.super_view = nil;
            if (block) {
                block();
            }else {
                if (self.closeBlock) {
                    self.closeBlock();
                }
            }
        });
    }else {
        [self removeFromSuperview];
        self.super_view = nil;
        if (block) {
            block();
        }else {
            if (self.closeBlock) {
                self.closeBlock();
            }
        }
    }
}

@end
